/** 
 
*/
import React, { PureComponent } from 'react';
import { connect } from 'dva';
import { FirstHeaderC, NoDataMaskC } from '../../../../comps';
import { fixedNum, formSelectData } from '../../../../utils/utils';
import { Row, Col, Tooltip } from 'antd';
import accounting from 'accounting';
import config from '../../../../comps/business/tradingMarket/config';
import 'moment/locale/zh-cn';

import myStyle from './style.less';
class finaFlockingInfoC extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      baseInfo: undefined, // 理财直融工具基础信息
      masking: true, // 蒙版
      zjUseHyList: [],
    };
  }

  componentDidMount() {
    const { info } = this.props;
    if (info) {
      let tempInfo = this.changeInfoData(info);
      this.setState({
        baseInfo: tempInfo,
        masking: false,
      });
    }
    const {
      globalM: {
        dataDictionary: { COUNTER_PARTY_RATE_ORGAN = [], GJTJJHYFLI = [] },
      },
    } = this.props;
    // 查询评级字典
    this.setState({
      agencyList: formSelectData(COUNTER_PARTY_RATE_ORGAN),
      zjUseHyList: GJTJJHYFLI || [],
    });
  }

  UNSAFE_componentWillReceiveProps(nextProps) {
    const { info } = this.props;
    if (nextProps.info) {
      if (JSON.stringify(info) !== JSON.stringify(nextProps.info)) {
        let tempInfo = this.changeInfoData(nextProps.info);
        this.setState({
          baseInfo: tempInfo,
          masking: false,
        });
      }
    } else {
      this.setState({
        baseInfo: undefined,
        masking: true,
      });
    }
  }

  getFinanceTrade = (e) => {
    let find = [];
    let title;
    this.state.zjUseHyList.map((item) => {
      if (item.children && item.children.length) {
        find = item.children.filter((i) => i.code == e);
        if (find.length) {
          title = find[0].value;
        }
      }
    });
    return title;
  };

  getRateOrgan = (e) => {
    let find = this.state.agencyList.filter((item) => e == item.value);
    return find.length ? find[0].title : e;
  };

  showInfo = () => {
    const { baseInfo, masking } = this.state;
    // isRule 工具现金流是否规则
    // interestDistri 利息分配方式
    // interestUnit 频率单位
    const ruleArr = [
      { value: '0', title: '规则' },
      { value: '1', title: '不规则' },
    ];
    const interestDistriArr = [
      { value: '0', title: '平均分配' },
      { value: '1', title: '按天数分配' },
    ];
    const interestUnitArr = [
      { value: 'Y', title: '年' },
      { value: 'M', title: '月' },
      { value: 'D', title: '日' },
    ];

    return (
      <div style={{ position: 'relative', marginBottom: '10px' }}>
        {/* *******************************第一行************************************* */}
        <Row className={myStyle.fina_list}>
          <Col span={4} className={myStyle.finaItem}>
            <span className={myStyle.spanLeft}>交易市场:</span>
          </Col>
          <Col span={8} className={myStyle.finaItem}>
            <span className={myStyle.spanRight}>
              {baseInfo
                ? baseInfo['exchangeCode']
                  ? config.map((item) => {
                      if (item.value == baseInfo.exchangeCode) {
                        return item.title;
                      }
                    })
                  : '--'
                : '--'}
            </span>
          </Col>
          <Col span={4} className={myStyle.finaItem}>
            <span className={myStyle.spanLeft}>证券代码:</span>
          </Col>
          <Col span={8} className={myStyle.finaItem}>
            <span className={myStyle.spanRight}>{baseInfo ? (baseInfo['secucode'] ? baseInfo.secucode : '--') : '--'}</span>
          </Col>
        </Row>
        {/* *******************************第二行************************************* */}
        <Row className={myStyle.fina_list}>
          <Col span={4} className={myStyle.finaItem}>
            <span className={myStyle.spanLeft}>证券简称:</span>
          </Col>
          <Col span={8} className={myStyle.finaItem}>
            <span className={myStyle.spanRight}>{baseInfo ? (baseInfo['secuabbr'] ? baseInfo.secuabbr : '--') : '--'}</span>
          </Col>

          <Col span={4} className={myStyle.finaItem}>
            <span className={myStyle.spanLeft}>证券全称:</span>
          </Col>
          <Col span={8} className={myStyle.finaItem}>
            <Tooltip title={baseInfo ? (baseInfo['chiname'] ? baseInfo.chiname : '') : ''}>
              <span className={myStyle.spanRight}>{baseInfo ? (baseInfo['chiname'] ? baseInfo.chiname : '--') : '--'}</span>
            </Tooltip>
          </Col>
        </Row>
        {/* *******************************第三行************************************* */}
        <Row className={myStyle.fina_list}>
          <Col span={4} className={myStyle.finaItem}>
            <span className={myStyle.spanLeft}>融资人:</span>
          </Col>
          <Col span={8} className={myStyle.finaItem}>
            <Tooltip title={baseInfo ? (baseInfo['financeName'] ? baseInfo.financeName : '') : ''}>
              <span className={myStyle.spanRight} title={baseInfo ? (baseInfo['financeName'] ? baseInfo.financeName : '') : ''}>
                {baseInfo ? (baseInfo['financeName'] ? baseInfo.financeName : '--') : '--'}
              </span>
            </Tooltip>
          </Col>

          <Col span={4} className={myStyle.finaItem}>
            <span className={myStyle.spanLeft}>融资人所属行业:</span>
          </Col>
          <Col span={8} className={myStyle.finaItem}>
            <Tooltip title={baseInfo ? (baseInfo['financeTrade'] ? this.getFinanceTrade(baseInfo.financeTrade) : '') : ''}>
              <span className={myStyle.spanRight}>{baseInfo ? (baseInfo['financeTrade'] ? this.getFinanceTrade(baseInfo.financeTrade) : '--') : '--'}</span>
            </Tooltip>
          </Col>
        </Row>
        {/* *******************************第四行************************************* */}
        <Row className={myStyle.fina_list}>
          <Col span={4} className={myStyle.finaItem}>
            <span className={myStyle.spanLeft}>是否为城投企业:</span>
          </Col>
          <Col span={8} className={myStyle.finaItem}>
            <span className={myStyle.spanRight}>{baseInfo ? (baseInfo['isCity'] ? baseInfo.isCity : '--') : '--'}</span>
          </Col>
          <Col span={4} className={myStyle.finaItem}>
            <span className={myStyle.spanLeft}>期限:</span>
          </Col>
          <Col span={8} className={myStyle.finaItem}>
            <span className={myStyle.spanRight}>{baseInfo ? (baseInfo['term'] ? baseInfo.term : '--') : '--'}</span>
          </Col>
        </Row>
        {/* *******************************第五行************************************* */}
        <Row className={myStyle.fina_list}>
          <Col span={4} className={myStyle.finaItem}>
            <span className={myStyle.spanLeft}>期限单位:</span>
          </Col>
          <Col span={8} className={myStyle.finaItem}>
            <span className={myStyle.spanRight}>{baseInfo ? (baseInfo['company'] ? baseInfo.company : '--') : '--'}</span>
          </Col>
          <Col span={4} className={myStyle.finaItem}>
            <span className={myStyle.spanLeft}>计划发行规模:</span>
          </Col>
          <Col span={8} className={myStyle.finaItem}>
            <span className={myStyle.spanRight}>{baseInfo ? (baseInfo['total'] ? fixedNum(baseInfo.total, 2) + '万元' : '--') : '--'}</span>
          </Col>
        </Row>
        {/* *******************************第六行************************************* */}
        <Row className={myStyle.fina_list}>
          <Col span={4} className={myStyle.finaItem}>
            <span className={myStyle.spanLeft}>实际发行规模:</span>
          </Col>
          <Col span={8} className={myStyle.finaItem}>
            <span className={myStyle.spanRight}>{baseInfo ? (baseInfo['actualTotal'] ? fixedNum(baseInfo.actualTotal, 2) + '万元' : '--') : '--'}</span>
          </Col>
          <Col span={4} className={myStyle.finaItem}>
            <span className={myStyle.spanLeft}>发行价格:</span>
          </Col>
          <Col span={8} className={myStyle.finaItem}>
            <span className={myStyle.spanRight}>{baseInfo ? (baseInfo['issueprice'] ? baseInfo.issueprice + '元' : '--') : '--'}</span>
          </Col>
        </Row>
        {/* *******************************第七行************************************* */}
        <Row className={myStyle.fina_list}>
          <Col span={4} className={myStyle.finaItem}>
            <span className={myStyle.spanLeft}>上市流通日:</span>
          </Col>
          <Col span={8} className={myStyle.finaItem}>
            <span className={myStyle.spanRight}>{baseInfo ? (baseInfo['listingdate'] ? baseInfo.listingdate : '--') : '--'}</span>
          </Col>
          <Col span={4} className={myStyle.finaItem}>
            <span className={myStyle.spanLeft}>主体评级机构:</span>
          </Col>
          <Col span={8} className={myStyle.finaItem}>
            <span className={myStyle.spanRight}>{baseInfo ? (baseInfo['rateOrgan'] ? this.getRateOrgan(baseInfo.rateOrgan) : '--') : '--'}</span>
          </Col>
        </Row>
        {/* *******************************第八行************************************* */}
        <Row className={myStyle.fina_list}>
          <Col span={4} className={myStyle.finaItem}>
            <span className={myStyle.spanLeft}>融资人主体评级:</span>
          </Col>
          <Col span={8} className={myStyle.finaItem}>
            <span className={myStyle.spanRight}>{baseInfo ? (baseInfo['rate'] ? baseInfo.rate : '--') : '--'}</span>
          </Col>
          <Col span={4} className={myStyle.finaItem}>
            <span className={myStyle.spanLeft}>主体评级日期:</span>
          </Col>
          <Col span={8} className={myStyle.finaItem}>
            <span className={myStyle.spanRight}>{baseInfo ? (baseInfo['rateDate'] ? baseInfo.rateDate : '--') : '--'}</span>
          </Col>
        </Row>
        {/* *******************************第九行************************************* */}
        <Row className={myStyle.fina_list}>
          <Col span={4} className={myStyle.finaItem}>
            <span className={myStyle.spanLeft}>资产评级:</span>
          </Col>
          <Col span={8} className={myStyle.finaItem}>
            <span className={myStyle.spanRight}>{baseInfo ? (baseInfo['toolRate'] ? baseInfo.toolRate : '--') : '--'}</span>
          </Col>
          <Col span={4} className={myStyle.finaItem}>
            <span className={myStyle.spanLeft}>票面利率:</span>
          </Col>
          <Col span={8} className={myStyle.finaItem}>
            <span className={myStyle.spanRight}>{baseInfo ? (baseInfo['couponRate'] ? baseInfo.couponRate : '--') : '--'}</span>
          </Col>
        </Row>
        {/* *******************************第十行************************************* */}
        <Row className={myStyle.fina_list}>
          <Col span={4} className={myStyle.finaItem}>
            <span className={myStyle.spanLeft}>计息频率:</span>
          </Col>
          <Col span={8} className={myStyle.finaItem}>
            <span className={myStyle.spanRight}>{baseInfo ? (baseInfo['interestFrequency'] ? baseInfo.interestFrequency + '次/年' : '--') : '--'}</span>
          </Col>
          <Col span={4} className={myStyle.finaItem}>
            <span className={myStyle.spanLeft}>利息分配方式:</span>
          </Col>
          <Col span={8} className={myStyle.finaItem}>
            <span className={myStyle.spanRight}>
              {baseInfo
                ? baseInfo['interestDistri']
                  ? interestDistriArr.map((item) => {
                      if (item.value == baseInfo.interestDistri) {
                        return item.title;
                      }
                    })
                  : '--'
                : '--'}
            </span>
          </Col>
        </Row>
        {/* *******************************第十一行************************************* */}
        <Row className={myStyle.fina_list}>
          <Col span={4} className={myStyle.finaItem}>
            <span className={myStyle.spanLeft}>现金流是否规则:</span>
          </Col>
          <Col span={8} className={myStyle.finaItem}>
            <span className={myStyle.spanRight}>
              {baseInfo
                ? baseInfo['isRule']
                  ? ruleArr.map((item) => {
                      if (item.value == baseInfo.isRule) {
                        return item.title;
                      }
                    })
                  : '--'
                : '--'}
            </span>
          </Col>
        </Row>
        {masking ? <NoDataMaskC height={'468px'} title={'请选择证券代码，加载数据！'} /> : null}
      </div>
    );
  };
  changeInfoData = (oldinfo) => {
    let tempObj = {};
    for (let k in oldinfo) {
      if (k == 'finterestDate' || k == 'rateDate' || k == 'listingdate') {
        //外部评级日期,计息日和上市流通日需要改为'2020-10-10'
        if (oldinfo[k]) {
          tempObj[k] = oldinfo[k].slice(0, 4) + '-' + oldinfo[k].slice(4, 6) + '-' + oldinfo[k].slice(6, 8);
          continue;
        }
      }
      if (k == 'twocycleY' || k == 'twocycleD') {
        //计息天数和计息年度天数需要取整加'天'
        if (oldinfo[k]) {
          tempObj[k] = parseInt(oldinfo[k]) + '天';
          continue;
        }
      }
      // if (k == 'total' || k == 'actualTotal') {
      //   // 计划发行总额和实际发行总额要换算成'万元' 千分位展示
      //   if (oldinfo[k]) {
      //     tempObj[k] = accounting.formatNumber(Number(oldinfo[k]) / 10000)
      //     continue;
      //   }
      // }
      if (k == 'issueprice') {
        // 发行价格 保留两位小数
        if (oldinfo[k]) {
          tempObj[k] = Number(oldinfo[k]).toFixed(2);
          continue;
        }
      }
      if (k == 'couponRate') {
        // 票面利率按百分比展示(乘以100)
        if (oldinfo[k]) {
          tempObj[k] = Number(oldinfo[k]).toFixed(2) + '%';
          continue;
        }
      }
      tempObj[k] = oldinfo[k];
    }
    return tempObj;
  };
  render() {
    const { masking } = this.state;
    return (
      <div>
        <FirstHeaderC title={'理财直融工具基础信息'} />
        {this.showInfo()}
      </div>
    );
  }
}
export default connect(({ finaFlockingM, globalM }) => ({
  finaFlockingM,
  globalM,
}))(finaFlockingInfoC);
